//
//  DECXLoginViewController.m
//  DECXInStore
//
//  Created by 程强 on 15/12/9.
//  Copyright © 2015年 程强. All rights reserved.
//

#import "DECXLoginViewController.h"
#import "DECXManager.h"
#import "DECXPrivacyPolicyController.h"

@interface DECXLoginViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) DECXManager *manager;
@property (weak, nonatomic) IBOutlet UILabel *accountTitleLbl;
@property (weak, nonatomic) IBOutlet UIButton *styleBtn;
@property (assign, nonatomic) BOOL isSeatLogin;
@end

@implementation DECXLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.manager = [DECXManager new];
    self.navigationItem.leftBarButtonItem = nil;
    
    NSString *account = [[NSUserDefaults standardUserDefaults]objectForKey:@"account"];
    NSString *pwd = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
    self.accountTf.text = account;
    self.pwdTf.text = pwd;
    self.isSeatLogin = NO;
}
- (void)gotoBack
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)changeLoginStyleClick:(id)sender
{
    if (!self.isSeatLogin) {
        self.accountTitleLbl.text = @"席位号";
        [self.styleBtn setTitle:@"切换用户名登录" forState:UIControlStateNormal];
        NSString *account = [[NSUserDefaults standardUserDefaults]objectForKey:@"seat"];
        NSString *pwd = [[NSUserDefaults standardUserDefaults]objectForKey:@"seat_password"];
        self.accountTf.text = account;
        self.pwdTf.text = pwd;
    }else{
        self.accountTitleLbl.text = @"用户名";
        [self.styleBtn setTitle:@"切换席位号登录" forState:UIControlStateNormal];
        
        NSString *account = [[NSUserDefaults standardUserDefaults]objectForKey:@"account"];
        NSString *pwd = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
        self.accountTf.text = account;
        self.pwdTf.text = pwd;
    }
    self.isSeatLogin = !self.isSeatLogin;
}
- (IBAction)login:(id)sender
{
    [self resignKeyBoard];
    if (self.isSeatLogin) {
        [self seatLogin];
    }else{
        [self accountLogin];
    }
}
- (void)accountLogin
{
    weak_Self(self);
    [self showHUD];
    [[self.manager login:self.accountTf.text pwd:self.pwdTf.text]subscribeNext:^(id x) {
        [DECXGlobal shareInstance].user = x;
        [weakSelf gotoBack];
        if (weakSelf.successBlock) {
            weakSelf.successBlock();
        }
        [DECXGlobal shareInstance].loginStatus = YES;
        if ([[DECXGlobal shareInstance].user.level isEqualToString:@"2"]) {
            [DECXGlobal shareInstance].loginType = @"1";
        }else if ([[DECXGlobal shareInstance].user.level isEqualToString:@"5"]){
            [DECXGlobal shareInstance].loginType = @"3";
        }
        [[NSUserDefaults standardUserDefaults]setObject:self.accountTf.text forKey:@"account"];
        [[NSUserDefaults standardUserDefaults]setObject:self.pwdTf.text forKey:@"password"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        [weakSelf hideHUD];
    }];

}
- (void)seatLogin
{
    weak_Self(self);
    [self showHUD];
    [[self.manager seatNoLogin:self.accountTf.text pwd:self.pwdTf.text]subscribeNext:^(id x) {
        [DECXGlobal shareInstance].user = x;
        [weakSelf gotoBack];
        if (weakSelf.successBlock) {
            weakSelf.successBlock();
        }
        [DECXGlobal shareInstance].loginStatus = YES;
        [DECXGlobal shareInstance].loginType = @"2";
        [[NSUserDefaults standardUserDefaults]setObject:self.accountTf.text forKey:@"seat"];
        [[NSUserDefaults standardUserDefaults]setObject:self.pwdTf.text forKey:@"seat_password"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [DECXGlobal shareInstance].user.seat = self.accountTf.text;
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        [weakSelf hideHUD];
    }];
}
- (void)resignKeyBoard
{
    [self.accountTf resignFirstResponder];
    [self.pwdTf resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self resignKeyBoard];
    return YES;
}
- (IBAction)protocalClick:(id)sender
{
    DECXPrivacyPolicyController *vc = [[DECXPrivacyPolicyController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
